from player import Player
from dealer import Dealer

class Parser:
	def __init__(self, table):
		u"EKG Parsing class"
		self.table = table
		self.player = table.players
		self.dealer = Dealer()
	def ParseMsg(self, text):
		cmd = ""
		param = ""
		if text[0] != "!":
			return #maybe an exception would be better
		else:
			text = text[1:]
			splittext = text.split(" ")
			if len(splittext[0]) > 0:
				cmd = splittext[0]
				for i in range(1, len(splittext)):
					if splittext[i] != ' ': #doesn't work very well, need further investigation
						param += splittext[i] + " "
				if len(param):
					param = param[:-1]
		return cmd, param
	def SenderIndex(self, uin):
		for i in range(len(self.player)):
			if self.player[i].uin == uin:
				return i
	def ProcessMsg(self, uin, name, text):
		si = self.SenderIndex(uin)
		cmd, param = self.ParseMsg(text)
		if cmd == "name":
			""
			#self.SetName(uin, param)
		elif cmd == "join":
			self.table.Sit(Player(), uin, name)
			si = self.SenderIndex(uin)
                        self.table.Broadcast("%s has joined the table" % self.player[si].name)
		elif cmd == "who":
			response = ""
			for i in range(len(self.table.players)): #lets move it to table method (return who list)
				p = self.player[i]
				response += "%d: %s (%d) money: $%d, ready=%s\n" % (i+1, p.name, p.uin, p.money, p.ready)
			self.player[si].Text(response)
		elif cmd == "ready":
			if self.player[si].ready:
				response = "You are still ready, nothing has changed"
				self.player[si].Text(response)
			else:
				self.player[si].ReadyUp()
				self.table.Broadcast("%s is ready" % self.player[si].name)
				ready_to_go = self.dealer.CheckPlayers(self.table)
				if ready_to_go:
					self.dealer.RetrieveBlinds(self.table)
					self.table.NextPhase()
		elif cmd == "leave":
			"leave"
		elif cmd == "call" or cmd == "check":
			if si == self.table.current_player:
				self.player[si].Call(self.table)
				self.table.Broadcast("%s checks/calls" % self.player[si].name)
				Dealer().NextPlayer(self.table)
			else:
				self.player[si].Text("Not your turn!")
		elif cmd == "pass":
                	if si == self.table.current_player:
				self.player[si].Pass()
				self.table.Broadcast("%s passes" % self.player[si].name)
				Dealer().NextPlayer(self.table)
			else:
				self.player[si].Text("Not your turn!")
		elif cmd == "raise" or cmd == "bet":
                	if si == self.table.current_player:
				self.player[si].Bet(int(param), self.table)
                        	Dealer().NextPlayer(self.table)
			else:
                        	self.player[si].Text("Not your turn!")
		elif cmd == "status":
			cards = self.table.cards #make it via getters and setters! nasty quick implementation
			self.player[si].Text("stol: %s reka: %s" % (cards, self.player[si].hand))
			self.player[si].Text("na stole jest obecnie: $%d ($%d to Twoja kasa)" % (self.table.pot, self.player[si].table))
		elif cmd == "start":
			"check if game is playable and start it"
			if self.table.Playable():
				""
			else:
				self.player[si].Text("Game is not playable at the moment")
		elif cmd == "help":
			"show help"
			response = """Basic commands are:
						!join - join the table
						!name - change name
						!who - show players
						!ready - ready up
						!leave - leave the table *(N/A)
						!call - equal amount of cash on the table
						!check - same as !call
						!pass - pass the bid
						!raise - place a bet or raise current bet
						!status - show the current situation
						!help - this help"""
			self.player[si].Text(response)
		else:
			raise UserWarning, "Command not found"
